/*
 * @Author: lwHao
 * @Date: 2020-07-06 14:40:59
 * @LastEditors: lwHao
 * @LastEditTime: 2020-07-06 21:38:38
 */
const sass = require('sass')
const loadGruntTasks = require('load-grunt-tasks')
const data = require('./configData')
module.exports = grunt => {
  grunt.initConfig({
    //grunt-contrib-clean 清除任务
    clean: {
      temp: ['temp', 'dist']
    },
    sass: {
      options: {
        implementation: sass
      },
      dist: {
        files: {
          'dist/assets/styles/main.css': 'src/assets/styles/main.scss'
        }
      }
    },
    babel: {
      options: {
        presets: ['@babel/preset-env']
      },
      main: {
        files: {
          'dist/assets/scripts/main.js': 'src/assets/scripts/main.js'
        }
      }
    },
    watch: {
      js: {
        files: ['src/assets/scripts/*.js'],
        tasks: ['babel'],
      },
      css: {
        files: ['src/assets/styles/*.scss'],
        tasks: ['sass']
      },
      html: {
        files: ['src/**/*.html'],
        tasks: ['web_swig']
      },
      image: {
        files: ['src/assets/images/**'],
        tasks: ['imagemin']
      },
      font: {
        files: ['src/assets/fonts/**'],
        tasks: ['imagemin']
      },
    },
    imagemin: {
      dist: {
        options: {
          optimizationLevel: 1 //定义 PNG 图片优化水平
        },
        files: [
          {
            expand: true,
            cwd: 'src/assets/images',
            src: ['**'],
            dest: 'dist/assets/images'
          }, {
            expand: true,
            cwd: 'src/assets/fonts',
            src: ['**'],
            dest: 'dist/assets/fonts'
          },
        ]
      }
    },
    web_swig: {
      options: {
        swigOptions: {
          cache: false
        },
        getData: function (tpl) {
          return data;
        }
      },
      html: {
        files: [
          {
            src: ['src/index.html'],
            dest: 'dist/index.html'
          }, {
            src: ['src/about.html'],
            dest: 'dist/about.html'
          }, {
            src: ['src/layouts/basic.html'],
            dest: 'dist/layouts/basic.html'
          }, {
            src: ['src/partials/footer.html'],
            dest: 'dist/partials/footer.html'
          }, {
            src: ['src/partials/header.html'],
            dest: 'dist/partials/header.html'
          },
        ],
      }
    },
    extra: {
      options: {
        rootDir: 'public',
        dest: 'dist'
      }
    },
    browserSync: {
      bsFiles: {
        src: [
          'public/**',
          'dist/**',
          'src/assets/fonts/**',
          'src/assets/images/**'
        ]
      },
      options: {
        port: 8080,
        watchTask: true,
        server: {
          baseDir: ['dist', 'src', 'public'],
          routes: {
            '/node_modules': 'node_modules'
          }
        }
      }
    },
    useref: {
      html:'dist/**/*.html',
      temp:'dist'
    }
  })
  grunt.loadNpmTasks('grunt-contrib-clean')
  grunt.loadNpmTasks('grunt-sass')
  grunt.loadNpmTasks('grunt-babel')
  grunt.loadNpmTasks('grunt-contrib-watch')
  grunt.loadNpmTasks('grunt-contrib-imagemin')
  grunt.loadNpmTasks('grunt-web-swig')
  grunt.loadNpmTasks('grunt-browser-sync');
  grunt.loadNpmTasks('grunt-useref');
  // grunt.registerTask('clean','清除所有编译后的文件')
  grunt.registerTask('extra', function () {
    const { dest, rootDir } = this.options()
    grunt.file.recurse(rootDir, (abspath, rootdir, subdir, filename) => {
      let relPath = abspath.replace(`public`, '')
      grunt.file.copy(abspath, `${dest}${relPath}`)
    })
  })

  grunt.registerTask('develop', '启动开发环境编译', ['sass', 'babel', 'web_swig','browserSync','watch'])
  grunt.registerTask('build', '构建生产环境开发包', ['clean','sass', 'babel', 'web_swig','useref','concat', 'uglify', 'cssmin','imagemin','extra'])
}

